package com.market.controller.wx;



import com.market.annotation.AdminLogAnnotation;
import com.market.bean.MarketUser;
import com.market.bean.common.BaseRespVo;
import com.market.bean.vo.wx.UserInfoBeanBo;
import com.market.bean.vo.wx.UserLoginBo;
import com.market.config.realm.MarketToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

//shiro整合之后，在做具体的开发
//响应结果都是JSON，Controller组件上我们都用@RestController注解
@RestController
@RequestMapping("wx/auth")
public class WxAuthController {

    /**
     * Shiro安全框架 → 视频课程 4个课时 → 认证（登录）
     * 如果参数比较少，类型比较简单的话，使用map来接收也可以
     */
    @PostMapping("login")
    public BaseRespVo<UserLoginBo> login(@RequestBody Map map) {
        String username = (String)map.get("username");
        String password = (String)map.get("password");

        // 写业务
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken authenticationToken = new MarketToken(username, password, "wx");
        try {
            subject.login(authenticationToken);
        }catch (AuthenticationException e){
            return BaseRespVo.error();
        }
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Session session = subject.getSession();

        UserInfoBeanBo userInfoBeanBo = new UserInfoBeanBo();
        userInfoBeanBo.setAvatarUrl(marketUser.getAvatar());
        userInfoBeanBo.setNickName(marketUser.getUsername());
        UserLoginBo userLoginBo = new UserLoginBo();
        userLoginBo.setUserInfo(userInfoBeanBo);

        userLoginBo.setToken((String) session.getId());

       /* LoginUserData loginUserData = new LoginUserData();
        AdminInfoBean adminInfo = new AdminInfoBean();
        adminInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        adminInfo.setNickName("admin123");
        loginUserData.setAdminInfo(adminInfo);
        loginUserData.setToken("c4d988f3-e4c5-4e46-b6ce-13b9e008ea20");*/
        return BaseRespVo.ok(userLoginBo);
    }



    @RequestMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }


   /* @RequestMapping("info")
    public BaseRespVo info(String token) {

        for (int i = 0; i < 10000; i++) {
            //想要在第6527次的位置这里的断点停一下
            System.out.println("次数：" + i);
        }

        //开发完shiro之后，再整合
        InfoData infoData = new InfoData();
        infoData.setName("admin123");


        //根据primaryPrincipal做查询
        infoData.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        ArrayList<String> roles = new ArrayList<>();
        roles.add("超级管理员");
        infoData.setRoles(roles);
        ArrayList<String> perms = new ArrayList<>();
        perms.add("*");
        infoData.setPerms(perms);


        return BaseRespVo.ok(infoData);
    }*/
}
